//
//  MoUIStatus.swift
//  doulive
//
//  Created by zhuxietong on 2020/2/26.
//  Copyright © 2020 douyun. All rights reserved.
//

import Foundation
import UIKit
import Alamofire
import RxSwift

//extension UIImage{
//    public convenience init?(_snamed:String) {
//        let bundle = Bundle(for: MoUIAdapter.self)
//        self.init(named: _snamed, in: bundle, compatibleWith: nil)
//    }
//}
extension UIImage{
    public convenience init?(_snamed:String) {
        var bundle = Bundle(for: MoUIAdapter.self)
        if let path = bundle.path(forResource: "soappAsset", ofType: "bundle"){
            if let b = Bundle(path: path){
                bundle = b
            }
        }
        self.init(named: _snamed, in: bundle, compatibleWith: nil)
    }
}

//extension UIImage{
//    public convenience init?(_named:String) {
//        var bundle = Bundle(for: MoUIAdapter.self)
//        if let path = bundle.path(forResource: "molive", ofType: "bundle"){
//            if let b = Bundle(path: path){
//                bundle = b
//            }
//        }
//        self.init(named: zb_named, in: bundle, compatibleWith: nil)
//    }
//}




public class MoUIAdapter{
    public static var safeInset:UIEdgeInsets{
        get{
            var back:UIView? = UIApplication.shared.keyWindow
            if back == nil{
                back = UIApplication.shared.keyWindow?.rootViewController?.view
            }
            if #available(iOS 11.0, *) {
                return back?.safeAreaInsets ?? .zero
            } else {
                return .zero
                // Fallback on earlier versions
            }
        }
    }
    
    public static func bottom(_ distance:CGFloat) ->CGFloat{
        let b = MoUIAdapter.safeInset.bottom
        return max(b,distance)
    }
    
}





extension UIApplication{
    public static var active_:PublishSubject<Active>? = nil
    public static var rxActive:PublishSubject<Active>{
        guard let ac = active_ else {
            let names:[Notification.Name] = [
                UIApplication.didEnterBackgroundNotification,
                UIApplication.willEnterForegroundNotification,
                UIApplication.willResignActiveNotification,
                UIApplication.didBecomeActiveNotification
            ]
            UNUserNotificationCenter.current().rx
            names.forEach { (name) in
                NotificationCenter.default.addObserver(self.classForCoder(), selector: #selector(reciveNotification(notification:)), name: name, object: nil)
            }
            let active = PublishSubject<Active>()
            UIApplication.active_ = active
            return active
        }
        return ac
    }
    
    public enum Active {
        case didEnterBackGround
        case willEnterForeground
        case willResignActive
        case didBecomeActive
        static func getActive(notification:Notification)->Active?{
            switch notification.name {
            case UIApplication.didEnterBackgroundNotification:
                return .didEnterBackGround
            case UIApplication.willEnterForegroundNotification:
                return .willEnterForeground
            case UIApplication.willResignActiveNotification:
                return .willResignActive
            case UIApplication.didBecomeActiveNotification:
                return .didBecomeActive
            default:
                return nil
            }
        }
    }
    @objc static func reciveNotification(notification:Notification){
        let active = Active.getActive(notification: notification)
        if let ac = active{
            UIApplication.rxActive.onNext(ac)
        }
    }
}
//public class MoActive{
//    let active:PublishSubject<Active> = PublishSubject<Active>()
//
//
//    init() {
//        let names:[Notification.Name] = [
//            UIApplication.didEnterBackgroundNotification,
//            UIApplication.willEnterForegroundNotification,
//            UIApplication.willResignActiveNotification,
//            UIApplication.didBecomeActiveNotification
//        ]
//        names.forEach { (name) in
//            NotificationCenter.default.addObserver(self, selector: #selector(reciveNotification(notification:)), name: name, object: nil)
//        }
//    }
//
//
//
//
//}

public class MoUIStatus: NSObject {
    
    public class Keyboard{
        var tag:Any? = nil
        
        public init(tag:Any?) {
            self.tag = tag
            addObserver()
        }
        deinit {
            NotificationCenter.default.removeObserver(self)
        }
        
        public var change:(_:Status) ->Void = {_ in}
        public var status = Status.didHide(Notification(name: "".notice_name)){
            didSet{
                change(status)
            }
        }
        
        public enum Status {
            case willShow(CGRect,Notification)
            case willHide(CGRect,Notification)
            case didHide(Notification)
            case didShow(CGRect,Notification)
            case changeFrame(CGRect,Notification)
            public var notification:Notification{
                get{
                    switch self {
                    case .willHide(_, let n),.changeFrame(_, let n),.didHide(let n),.didShow(_, let n),.willShow(_, let n):
                        return n
                    }
                }
            }
        }
        public var frame:CGRect = .zero
        public var filterSameFrame = true
        
        func addObserver() {
            NotificationCenter.default.addObserver(self, selector: #selector(keyboardChange(_:)), name: UIResponder.keyboardWillShowNotification, object: nil)
                   NotificationCenter.default.addObserver(self, selector: #selector(keyboardChange(_:)), name: UIResponder.keyboardWillHideNotification, object: nil)
                   NotificationCenter.default.addObserver(self, selector: #selector(keyboardChange(_:)), name: UIResponder.keyboardWillChangeFrameNotification, object: nil)
                   NotificationCenter.default.addObserver(self, selector: #selector(keyboardChange(_:)), name: UIResponder.keyboardDidHideNotification, object: nil)
                   NotificationCenter.default.addObserver(self, selector: #selector(keyboardChange(_:)), name: UIResponder.keyboardDidShowNotification, object: nil)

        }
        @objc func keyboardChange(_ notify:Notification)
        {
//            print("keyboardChange------",notify)
            let userInfo = notify.userInfo
            var animationDuration:TimeInterval = 0.25
            var keyboardEndFrame:CGRect = CGRect(x: 0, y: 0, width: 0, height: 0)
            let animationDuration_v = userInfo![UIResponder.keyboardAnimationDurationUserInfoKey] as! Double
            let curve = userInfo![UIResponder.keyboardAnimationCurveUserInfoKey] as! Int

            animationDuration = animationDuration_v as TimeInterval
            keyboardEndFrame = (userInfo![UIResponder.keyboardFrameEndUserInfoKey] as! NSValue).cgRectValue
            _ = keyboardEndFrame.size.height
            let h = keyboardEndFrame.size.height
            switch notify.name {
            case UIResponder.keyboardWillShowNotification:
               let oldH = frame.size.height
               if (oldH == h && filterSameFrame){return}
               UIView.beginAnimations(nil, context: nil)
               UIView.setAnimationDuration(animationDuration)
               UIView.setAnimationCurve(UIView.AnimationCurve.init(rawValue: curve) ?? .easeIn)
               frame = keyboardEndFrame
               status = .willShow(keyboardEndFrame, notify)
               UIView.commitAnimations()
            case UIResponder.keyboardWillChangeFrameNotification:
                let oldH = frame.size.height
                if (oldH == h && filterSameFrame){return}
               
                UIView.beginAnimations(nil, context: nil)
                UIView.setAnimationDuration(animationDuration)
                UIView.setAnimationCurve(UIView.AnimationCurve.init(rawValue: curve) ?? .easeIn)
                frame = keyboardEndFrame
                status = .changeFrame(keyboardEndFrame, notify)
                UIView.commitAnimations()

            case UIResponder.keyboardWillHideNotification:
               
                UIView.beginAnimations(nil, context: nil)
                UIView.setAnimationDuration(animationDuration)
                UIView.setAnimationCurve(UIView.AnimationCurve.init(rawValue:curve) ?? .easeIn)
                frame = .zero
                status = .willHide(.zero, notify)
                UIView.commitAnimations()
    
               
            case UIResponder.keyboardDidHideNotification:
                frame = .zero
                status = .didHide(notify)
                
            default: break
                
            }
        }
    }
    
    
    var _hasActive = false
    public enum Appear{
        case willAppear
        case didAppear
        case didDisappear
        case willDisappear
    }
    
 


    
   
    

       
    deinit {
        NotificationCenter.default.removeObserver(self)
    }
    
    
 
    
    
    
    
}

